home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / examples / Text.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1991-11-28  |  10.9 KB  |  357 lines

  1. ; -*-Lisp-*-
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;
  4. ; File:         text.lsp
  5. ; RCS:          $Header: Text.lsp,v 1.7 91/10/05 04:51:15 mayer Exp $
  6. ; Description:  Random experiments with the Motif text editor widget.
  7. ; Author:       Niels Mayer, HPLabs
  8. ; Created:      Sat Nov 25 01:30:56 1989
  9. ; Modified:     Thu Nov 21 20:00:36 1991
  10. ; Language:     Lisp
  11. ; Package:      N/A
  12. ; Status:       X11r5 contrib tape release
  13. ;
  14. ; WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. ; XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. ;
  17. ; Permission to use, copy, modify, distribute, and sell this software and its
  18. ; documentation for any purpose is hereby granted without fee, provided that
  19. ; the above copyright notice appear in all copies and that both that
  20. ; copyright notice and this permission notice appear in supporting
  21. ; documentation, and that the name of Hewlett-Packard and Niels Mayer not be
  22. ; used in advertising or publicity pertaining to distribution of the software
  23. ; without specific, written prior permission.  Hewlett-Packard and Niels Mayer
  24. ; makes no representations about the suitability of this software for any
  25. ; purpose.  It is provided "as is" without express or implied warranty.
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27.  
  28. (defun find-file (file)
  29.   (let*
  30.       (;; loc vars
  31.        (top_w
  32.     (send TOP_LEVEL_SHELL_WIDGET_CLASS :new "edshl"
  33.           :XMN_GEOMETRY "500x500+1+1"
  34.           :XMN_TITLE file
  35.           :XMN_ICON_NAME file
  36.           ))
  37.        (te_w
  38.     (send XM_TEXT_WIDGET_CLASS :new :managed :scrolled "text" top_w
  39.           :XMN_EDIT_MODE :MULTI_LINE_EDIT    
  40. ;;;          :XMN_FONT_LIST "hp8.10x20b"
  41.           ))
  42.        (fp
  43.     (open file :direction :input)
  44.     )
  45.        inspos
  46.        text_line
  47.        )
  48.  
  49.  
  50.     (if (null fp)
  51.     (error "Can't open file." file))
  52.  
  53.     (send te_w :replace 0 0 (read-line fp))
  54.  
  55.     (loop
  56.      (if (null (setq text_line (read-line fp)))
  57.      (return))
  58.      (setq inspos (send te_w :get_insertion_position))
  59.      (send te_w :replace inspos inspos (strcat "\n" text_line))
  60.      )
  61.     (close fp)
  62.     (send top_w :realize)
  63.     te_w                ;return value
  64.     )
  65.   )
  66.  
  67. (setq tw (find-file "/etc/passwd"))
  68.  
  69. (send tw :set_callback :XMN_LOSING_FOCUS_CALLBACK
  70.       '(CALLBACK_WIDGET
  71.     CALLBACK_REASON
  72.     CALLBACK_XEVENT
  73.     CALLBACK_DOIT
  74.     CALLBACK_CUR_INSERT
  75.     CALLBACK_NEW_INSERT
  76.     CALLBACK_START_POS
  77.     CALLBACK_END_POS
  78. ;;;    CALLBACK_TEXT
  79.     )
  80.       '((format T "widget=~A\nreason=~A\nxevent=~A\n" 
  81.         CALLBACK_WIDGET    CALLBACK_REASON    CALLBACK_XEVENT)
  82.     (format T "cur_insert=~A\nnew_insert=~A\n" 
  83.         CALLBACK_CUR_INSERT CALLBACK_NEW_INSERT)
  84.     (format T "start_pos=~A\nend_pos=~A\n"
  85.         CALLBACK_START_POS CALLBACK_END_POS)
  86.     (format T "doit=~A\n"
  87.         CALLBACK_DOIT)
  88. ;;;    (setq CALLBACK_DOIT nil)
  89.     ))
  90.  
  91. (send tw :set_callback :XMN_MODIFY_VERIFY_CALLBACK
  92.       '(CALLBACK_WIDGET
  93.     CALLBACK_REASON
  94.     CALLBACK_XEVENT
  95.     CALLBACK_DOIT
  96.     CALLBACK_CUR_INSERT
  97.     CALLBACK_NEW_INSERT
  98.     CALLBACK_START_POS
  99.     CALLBACK_END_POS
  100.     CALLBACK_TEXT
  101.     )
  102.       '((format T "widget=~A\nreason=~A\nxevent=~A\n" 
  103.         CALLBACK_WIDGET    CALLBACK_REASON    CALLBACK_XEVENT)
  104.     (format T "cur_insert=~A\nnew_insert=~A\n" 
  105.         CALLBACK_CUR_INSERT CALLBACK_NEW_INSERT)
  106.     (format T "start_pos=~A\nend_pos=~A\n"
  107.         CALLBACK_START_POS CALLBACK_END_POS)
  108.     (format T "doit=~A\ntext='~A'\n"
  109.         CALLBACK_DOIT CALLBACK_TEXT)
  110. ;;;    (setq CALLBACK_DOIT nil)
  111.     ))
  112.  
  113.  
  114. (send tw :set_callback :XMN_MOTION_VERIFY_CALLBACK
  115.       '(CALLBACK_WIDGET
  116.     CALLBACK_REASON
  117.     CALLBACK_XEVENT
  118.     CALLBACK_DOIT
  119.     CALLBACK_CUR_INSERT
  120.     CALLBACK_NEW_INSERT
  121. ;;;    CALLBACK_START_POS
  122. ;;;    CALLBACK_END_POS
  123. ;;;    CALLBACK_TEXT
  124.     )
  125.       '((format T "widget=~A\nreason=~A\nxevent=~A\n" 
  126.         CALLBACK_WIDGET    CALLBACK_REASON    CALLBACK_XEVENT)
  127.     (format T "cur_insert=~A\nnew_insert=~A\n" 
  128.         CALLBACK_CUR_INSERT CALLBACK_NEW_INSERT)
  129.     (format T "doit=~A\n"
  130.         CALLBACK_DOIT)
  131. ;;;    (setq CALLBACK_DOIT nil)
  132.     ))
  133.  
  134.  
  135. (send tw :set_callback :XMN_VALUE_CHANGED_CALLBACK
  136.       '(CALLBACK_WIDGET
  137.     CALLBACK_REASON
  138.     CALLBACK_XEVENT
  139. ;;;    CALLBACK_DOIT
  140. ;;;    CALLBACK_CUR_INSERT
  141. ;;;    CALLBACK_NEW_INSERT
  142. ;;;    CALLBACK_START_POS
  143. ;;;    CALLBACK_END_POS
  144. ;;;    CALLBACK_TEXT
  145.     )
  146.       '(
  147.     (format T "widget=~A\nreason=~A\nxevent=~A\n" 
  148.         CALLBACK_WIDGET    CALLBACK_REASON    CALLBACK_XEVENT)
  149.     ))
  150.  
  151.  
  152. (send tw :set_callback :XMN_FOCUS_CALLBACK
  153.       '(CALLBACK_WIDGET
  154.     CALLBACK_REASON
  155.     CALLBACK_XEVENT
  156. ;;;    CALLBACK_DOIT
  157. ;;;    CALLBACK_CUR_INSERT
  158. ;;;    CALLBACK_NEW_INSERT
  159. ;;;    CALLBACK_START_POS
  160. ;;;    CALLBACK_END_POS
  161. ;;;    CALLBACK_TEXT
  162.     )
  163.       '(
  164.     (format T "widget=~A\nreason=~A\nxevent=~A\n" 
  165.         CALLBACK_WIDGET    CALLBACK_REASON    CALLBACK_XEVENT)
  166.     ))
  167.  
  168. (let ()
  169. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  170. ;; Add a :FIND_FILE method to the Motif Text widget.
  171. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  172. (send XM_TEXT_WIDGET_CLASS :answer :FIND_FILE '(filename linenum)
  173.       '(
  174.     (let*
  175.         (;; loc vars
  176.          (fp
  177.           (open filename :direction :input)
  178.           )
  179.          inspos
  180.          text_line
  181.          )
  182.  
  183.       (if (null fp)
  184.           (error "Can't open file." filename))
  185.  
  186.       (send self :set_string "")    ;clear out old text
  187.       (send self :disable_redisplay NIL) ;don't show changes till done
  188.       (loop
  189.        (if (null (setq text_line (read-line fp)))
  190.            (return))
  191.        (setq inspos (send self :get_insertion_position))
  192.        (send self :replace inspos inspos (strcat text_line "\n"))
  193.        )
  194.  
  195.       (send self :scroll linenum)    ;make <linenum> be the top of screen
  196.  
  197.       (send self :enable_redisplay)    ;now show changes...
  198.  
  199.       (close fp)
  200.       )
  201.     )
  202.       )
  203.  
  204. (setq toplevel_w
  205.       (send TOP_LEVEL_SHELL_WIDGET_CLASS :new "ed2shl"
  206.         ))
  207. (setq rc_w
  208.       (send XM_ROW_COLUMN_WIDGET_CLASS :new :managed
  209.                "rc" toplevel_w
  210.                :XMN_ORIENTATION :VERTICAL
  211.                :XMN_PACKING :PACK_TIGHT
  212.                :XMN_ENTRY_ALIGNMENT :ALIGNMENT_CENTER
  213.                ))
  214. (setq te_line_w
  215.       (send XM_TEXT_WIDGET_CLASS :new :managed rc_w
  216.         :XMN_EDIT_MODE :SINGLE_LINE_EDIT
  217.         ))
  218.  
  219. (send te_line_w :set_callback :XMN_ACTIVATE_CALLBACK '(CALLBACK_WIDGET)
  220.       '(
  221.     (send te_w :find_file (send CALLBACK_WIDGET :get_string) 0)
  222.     ))
  223.  
  224. (setq te_w
  225.       (send XM_TEXT_WIDGET_CLASS :new :managed :scrolled rc_w
  226.         :XMN_EDIT_MODE :MULTI_LINE_EDIT
  227.         :XMN_HEIGHT 400
  228.         :XMN_WIDTH  300
  229.         ))
  230.  
  231.  
  232.  
  233. (setq prevbut_w 
  234.       (send XM_PUSH_BUTTON_WIDGET_CLASS :new :managed rc_w
  235.         :XMN_LABEL_STRING "Prev Page"
  236.         ))
  237.  
  238. (send prevbut_w :set_callback :xmn_activate_callback '(CALLBACK_XEVENT)
  239.       '(
  240.     (send te_w :CALL_ACTION_PROC "previous-page"  CALLBACK_XEVENT)
  241.     )
  242.       )
  243.  
  244. (setq nextbut_w 
  245.       (send XM_PUSH_BUTTON_WIDGET_CLASS :new :managed rc_w
  246.         :XMN_LABEL_STRING "Next Page"
  247.         ))
  248.  
  249. (send nextbut_w :set_callback :xmn_activate_callback '(CALLBACK_XEVENT)
  250.       '(
  251.     (send te_w :CALL_ACTION_PROC "next-page"  CALLBACK_XEVENT)
  252.     )
  253.       )
  254.  
  255.  
  256. (setq insertbut_w 
  257.       (send XM_PUSH_BUTTON_WIDGET_CLASS :new :managed rc_w
  258.         :XMN_LABEL_STRING "Insert A String"
  259.         ))
  260.  
  261. (send insertbut_w :set_callback :xmn_activate_callback '(CALLBACK_XEVENT)
  262.       '(
  263.     (send te_w :CALL_ACTION_PROC "insert-string"  CALLBACK_XEVENT "funky " "hype " "bumrush " "jimmy's " "def " "fresh " "frop\n" )
  264.     )
  265.       )
  266.  
  267.  
  268. (send toplevel_w :realize)
  269. )
  270.  
  271. (send te_line_w :set_values :XMN_EDIT_MODE :MULTI_LINE_EDIT)
  272. (send te_line_w :get_values :XMN_EDIT_MODE nil)
  273. (send te_line_w :set_values :XMN_EDIT_MODE :SINGLE_LINE_EDIT)
  274. (send te_w :set_values :XMN_EDIT_MODE :SINGLE_LINE_EDIT)
  275. (send te_w :get_values :XMN_EDIT_MODE nil)
  276. (send te_w :set_values :XMN_EDIT_MODE :MULTI_LINE_EDIT)
  277.  
  278. (if (and (eq *MOTIF_VERSION* 1) (>= *MOTIF_REVISION* 1))
  279.     (send te_w :set_values        ;Motif >= 1.1
  280.       :xmn_selection_array #(:SELECT_POSITION
  281.                  :SELECT_WHITESPACE
  282.                  :SELECT_WORD
  283.                  :SELECT_LINE
  284.                  :SELECT_ALL
  285.                  :SELECT_PARAGRAPH)
  286.       :xmn_selection_array_count 6)
  287.   (send te_w :set_values        ;Motif 1.0
  288.     :xmn_selection_array #(:SELECT_POSITION
  289.                    :SELECT_WHITESPACE
  290.                    :SELECT_WORD
  291.                    :SELECT_LINE
  292.                    :SELECT_ALL)
  293.     :xmn_selection_array_count 5)
  294.   )
  295.  
  296.  
  297. ;;; (send te_line_w :set_callback :xmn_activate_callback
  298. ;;;       '(CALLBACK_WIDGET
  299. ;;;     CALLBACK_REASON
  300. ;;;     CALLBACK_XEVENT
  301. ;;; ;;;    CALLBACK_DOIT
  302. ;;; ;;;    CALLBACK_CUR_INSERT
  303. ;;; ;;;    CALLBACK_NEW_INSERT
  304. ;;; ;;;    CALLBACK_START_POS
  305. ;;; ;;;    CALLBACK_END_POS
  306. ;;; ;;;    CALLBACK_TEXT
  307. ;;;     )
  308. ;;;       '(
  309. ;;;     (format T "widget=~A\nreason=~A\nxevent=~A\n" 
  310. ;;;         CALLBACK_WIDGET    CALLBACK_REASON    CALLBACK_XEVENT)
  311. ;;;     (format T "contents of buffer='~A'\n" 
  312. ;;;         (send CALLBACK_WIDGET :GET_STRING))
  313. ;;;     ))
  314.  
  315. (send te_w :GET_STRING)
  316. (send te_w :GET_LAST_POSITION)
  317. (send te_w :SET_STRING
  318.       "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  319. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  320. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  321. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  322. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  323. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  324. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  325. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  326. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  327. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  328. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  329. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  330. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  331. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  332. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  333. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  334. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
  335. 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")
  336. (send te_w :REPLACE 3 7 "XXX")
  337. (send te_w :GET_EDITABLE)
  338. (send te_w :SET_EDITABLE nil)
  339. (send te_w :SET_EDITABLE t)
  340. (send te_w :GET_MAX_LENGTH)
  341. (send te_w :SET_MAX_LENGTH 9999999)
  342. (send te_w :GET_SELECTION)
  343. (send te_w :SET_SELECTION 3 7)
  344. (send te_w :CLEAR_SELECTION)
  345. (send te_w :GET_TOP_CHARACTER)
  346. (send te_w :SET_TOP_CHARACTER 0)
  347. (send te_w :GET_INSERTION_POSITION)
  348. (send te_w :SET_INSERTION_POSITION 202)
  349. (send te_w :SET_INSERTION_POSITION (send te_w :XY_TO_POS 40 100))
  350. (send te_w :GET_SELECTION_POSITION)
  351. (send te_w :XY_TO_POS 40 100)        ;weird? x,y values are in pixels!
  352. (send te_w :POS_TO_XY (send te_w :GET_INSERTION_POSITION))
  353. (send te_w :SHOW_POSITION 111)
  354. (send te_w :SCROLL -5)
  355. (send te_w :DISABLE_REDISPLAY t)
  356. (send te_w :ENABLE_REDISPLAY)
  357.